package cn.oveay.classdesign.controller.servlet;

import cn.oveay.classdesign.bean.basebean.Option;
import cn.oveay.classdesign.bean.basebean.Recode;
import cn.oveay.classdesign.bean.basebean.View;
import cn.oveay.classdesign.service.RecordService;
import cn.oveay.classdesign.service.SubjectService;
import cn.oveay.classdesign.service.UserService;
import cn.oveay.classdesign.service.impl.RecordServiceImpl;
import cn.oveay.classdesign.service.impl.SubjectServiceImpl;
import cn.oveay.classdesign.service.impl.UserServiceImpl;
import cn.oveay.classdesign.util.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@WebServlet("/list/view")
public class ViewServlet extends BaseServlet {
    private SubjectService subjectService = new SubjectServiceImpl();
    private UserService userService = new UserServiceImpl();
    private RecordService recordService = new RecordServiceImpl();

    public String forward(HttpServletRequest req, HttpServletResponse resp){
        String subjectId = req.getParameter("id");
        List<Option> options = subjectService.findById(subjectId).getOptions();
        List<Recode> recodes = recordService.findBySubjectId(subjectId);
        List<View> views = new ArrayList<>();
        Set<String> stringSet = new HashSet<>();
        for(Recode recode : recodes){
            stringSet.add(recode.getUserId());
        }
        for(String string : stringSet){
            View view = new View();
            view.setUserNam(userService.findById(string).getName());
            List<String> optionList = new ArrayList<>();
            for(Recode recode : recodes){
                 if(recode.getUserId().equals(string)){
                     for(Option option : options){
                        if(recode.getOptionId().equals(option.getId())){
                            optionList.add(option.getContent());
                        }
                     }
                 }
            }
            view.setOptions(optionList);
            views.add(view);
        }
        if(views.size() != 0){
            req.setAttribute("views", views);
        }
        return "/WEB-INF/pages/view.jsp";
    }
}
